<?php

    global $mikron;
    $mikron->Schema->Add(dirname(__FILE__).'/schema.xml', 'Схема задач');
    $mikron->OperatingInstructions->AddFunction("MIKRON_TASKS", "OperatingInstruction_MikronTasks");
    
    function OperatingInstruction_MikronTasks($parameters)
    {

        global $mikron;

        /**
        * Сохранение измененного состояния задачи
        */
        if(isset($_POST['taskeditor']))
        {
            $formdata = $_POST['taskeditor'];
            $task_id = (int)$formdata['taskid'];
            $taskstate = (int)$formdata['taskstate'];
            $task = new TASK($task_id);
            $task->state->value = $taskstate;
            $task->Save();
        }

        if(isset($_GET['opentask']))
        {
            $task_id = (int)$_GET['opentask'];
            return OpenTask($task_id);
        }

        $f = new html_form('tasklist', 'post', $_SERVER['REQUEST_URI']);
        $page = $f->pageindex;
        if($page == 2)
        {
            $task_id = (int)$f->formdata['taskid'];
            if(array_key_exists('newtask', $f->formdata))
            {
                return NewTask(null);
            }
        }
        $f->pageindex = 1;
            $t = new html_table(null, 'table4', '100%', 0, 2, Array('Название', 'Состояние'));
                $cr = new Criteria();
                $cr->Add(new Criterion('state', 3, CR_NOTEQUAL));
                $cr->Add(new Criterion('state', 4, CR_NOTEQUAL));
                $tasks = $mikron->Queries->Query('TASK', null, $cr);
                while($object = $tasks->fetch())
                {
                    if(!in_array($object->state->value, Array(3, 4)))
                    {
                        $t->newcol();
                            $adr = '?opentask='.$object->id;
                            $title = $object->title->value;
                            echo "<a href=\"$adr\">$title</a>";
                        $t->newcol($mikron->Enumerators->getEnumNameById('TASK_STATES', $object->state->value));
                    }
                }
            $t->end();
            $f->addSubmit('newtask', 'Создать');
        $f->end();

    }

    function NewTask()
    {
        if(!MikronClass::ClassIsFind('TASK'))
        {
            return false;
        }
        global $mikron;
        $task = new TASK();
        $mikron->EntityManager->ShowFormForUsers($task, 'Создание новой задачи:', false, false, $_SERVER['REQUEST_URI'], true, $_SERVER['REQUEST_URI'], null, null, null, null, $_SERVER['REQUEST_URI'], "Сохранить...");
    }

    function OpenTask($taskid)
    {
        if(!MikronClass::ClassIsFind('TASK'))
        {
            return false;
        }
        global $mikron;
        $tasks = $mikron->Queries->Query('TASK', (int)$taskid, null, null, null, null, false);
        if($tasks->count() != 1)
        {
            return;
        }
        $task = $tasks->fetch();
        $frm = new html_form('taskeditor', 'post', $_SERVER['REQUEST_URI'], null, 1);
            $frm->addInput(1, 'taskid', null, 0, 10, true, $task->id);
            $table = new html_table(2, null, '100%');
            $table->newcol('<h1>«'.htmlspecialchars($task->title->value).'»</h1>');
            $table->newcol(null, false, 'right');
            $frm->addSelectControlFromEnumerator(1, 'taskstate', 'Состояние',
                $mikron->Enumerators->getEnum('TASK_STATES'), $task->state->value);
            $frm->addSubmit('savetask', 'Сохранить');
            $table->newcol(nl2br(htmlspecialchars($task->text->value)));
            $table->end();
        $frm->end();
        echo '{{MIKRON_COMMENTS entity=TASK id='.$taskid.'
        caption="Комментарии и ответы:"}}';
    }

?>